/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DaftarKelasDialog.java
 *
 * Created on 09 Sep 14, 20:56:08
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.DaftarKelasDAO;
import com.ivanbiz.dao.impl.DaftarKelasDAOImpl;
import com.ivanbiz.model.AksesMatrix;
import com.ivanbiz.model.DaftarKelas;
import com.ivanbiz.model.Kelas;
import com.ivanbiz.service.GlobalSession;
import com.ivanbiz.service.MenuAksesConstant;
import com.ivanbiz.service.ServiceHelper;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Shbt Peterpan
 */
public class DaftarKelasDialog extends javax.swing.JDialog {

    DaftarKelas daftarKelas;
    DaftarKelasDAO daftarKelasDAO;
    List<DaftarKelas> listDaftarKelas;
    Kelas kelas;
    String lulus;

    public DaftarKelasDialog(Kelas kelas) {
        initComponents();
        daftarKelasDAO = new DaftarKelasDAOImpl();
        this.kelas = kelas;
//        renderButtonAkses(GlobalSession.getListAksesMatrix());
        textKelas.setText(kelas.getTransactionReference() + " (" + kelas.getDeskripsi() + ")");
        refresh(kelas);
        buttonKelulusan.setVisible(false);
    }

    public DaftarKelasDialog(Kelas kelas, List<DaftarKelas> listDaftarKelas) {
        initComponents();
        textKelas.setText(kelas.getTransactionReference() + " (" + kelas.getDeskripsi() + ")");
        this.listDaftarKelas = listDaftarKelas;
        updateTableDaftarKelasInvoice();
        buttonTambah.setVisible(false);
        buttonHapus.setVisible(false);
        buttonKelulusan.setVisible(false);
    }

    public DaftarKelasDialog(Kelas kelas, String lulus) {
        initComponents();
        this.lulus = lulus;
        this.kelas = kelas;
        daftarKelasDAO = new DaftarKelasDAOImpl();
        textKelas.setText(kelas.getTransactionReference() + " (" + kelas.getDeskripsi() + ")");
        refreshKelulusan(kelas);
        buttonTambah.setVisible(false);
        buttonHapus.setVisible(false);
        buttonKelulusan.setVisible(false);
        renderButtonAksesKelulusan(GlobalSession.getListAksesMatrix());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel10 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        textKelas = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tableDaftarKelas = new javax.swing.JTable();
        buttonTambah = new javax.swing.JButton();
        buttonHapus = new javax.swing.JButton();
        buttonKelulusan = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setModal(true);
        setResizable(false);

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel10.setText("Detail Daftar Kelas");

        jPanel5.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel11.setText("Kelas :");

        textKelas.setEditable(false);

        jLabel12.setText("Daftar Murid :");

        tableDaftarKelas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableDaftarKelas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableDaftarKelasMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tableDaftarKelas);

        buttonTambah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/tambah.jpg"))); // NOI18N
        buttonTambah.setText("Tambah Daftar Murid Baru");
        buttonTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTambahActionPerformed(evt);
            }
        });

        buttonHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/hapus.jpg"))); // NOI18N
        buttonHapus.setText("Hapus Daftar Murid Terseleksi");
        buttonHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonHapusActionPerformed(evt);
            }
        });

        buttonKelulusan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/kelulusan.jpg"))); // NOI18N
        buttonKelulusan.setText("Update Data");
        buttonKelulusan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonKelulusanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 754, Short.MAX_VALUE)
                    .addComponent(textKelas, javax.swing.GroupLayout.DEFAULT_SIZE, 754, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonTambah)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonHapus)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonKelulusan)))
                        .addGap(0, 176, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textKelas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(buttonKelulusan, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonTambah, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonHapus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        setSize(new java.awt.Dimension(816, 638));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTambahActionPerformed
        new MuridDialog(null, true, listDaftarKelas, kelas).setVisible(true);
        refresh(kelas);
}//GEN-LAST:event_buttonTambahActionPerformed

    private void buttonHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonHapusActionPerformed
        if (tableDaftarKelas.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan dihapus", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                daftarKelas = listDaftarKelas.get(tableDaftarKelas.getSelectedRow());
                daftarKelasDAO.delete(daftarKelas);
                refresh(kelas);
            } catch (Exception ex) {
                Logger.getLogger(DaftarKelasDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
}//GEN-LAST:event_buttonHapusActionPerformed

    private void buttonKelulusanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonKelulusanActionPerformed
        try {
            List<DaftarKelas> listDaftarkelasTemp = new ArrayList<DaftarKelas>();
            for (int x = 0; x < listDaftarKelas.size(); x++) {
                DaftarKelas daftarKelass = listDaftarKelas.get(x);
                if ((Boolean) tableDaftarKelas.getValueAt(x, 4)) {
                    daftarKelass.setChoose(true);
                    listDaftarkelasTemp.add(daftarKelass);
                } else {
                    listDaftarkelasTemp.add(daftarKelass);
                }
            }
            daftarKelasDAO.updateByKelulusan(listDaftarkelasTemp);
            refreshKelulusan(kelas);
        } catch (Exception ex) {
            Logger.getLogger(DaftarKelasDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_buttonKelulusanActionPerformed

    private void tableDaftarKelasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableDaftarKelasMouseClicked
        if (lulus != null) {
            if ((Boolean) tableDaftarKelas.getValueAt(tableDaftarKelas.getSelectedRow(), 0)) {
                new DaftarKelasUpdateDataDialog(null, true, listDaftarKelas.get(tableDaftarKelas.getSelectedRow())).setVisible(true);
                refreshKelulusan(kelas);
            }
        }
    }//GEN-LAST:event_tableDaftarKelasMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonHapus;
    private javax.swing.JButton buttonKelulusan;
    private javax.swing.JButton buttonTambah;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tableDaftarKelas;
    private javax.swing.JTextField textKelas;
    // End of variables declaration//GEN-END:variables

    private void refresh(Kelas kelas) {
        try {
            listDaftarKelas = daftarKelasDAO.getDataByEqualsOrderByBankAndNama(kelas.getTransactionReference());
            updateTableDaftarKelas();
        } catch (Exception ex) {
            Logger.getLogger(DaftarKelasDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void renderButtonAkses(List<AksesMatrix> listAksesMatrix) {
//        buttonTambah.setEnabled(MenuAksesConstant.validate(MenuAksesConstant.TAMBAH_DAFTAR_KELAS, listAksesMatrix));
//        buttonHapus.setEnabled(MenuAksesConstant.validate(MenuAksesConstant.HAPUS_DAFTAR_KELAS, listAksesMatrix));
    }

    private void updateTableDaftarKelas() {
        String[] judul = {"No", "Nama", "Bank"};
        Object[][] isi = new Object[listDaftarKelas.size()][3];
        int x = 0;
        int no = 0;
        for (DaftarKelas daftarKelass : listDaftarKelas) {
            no += 1;
            isi[x][0] = no;
            isi[x][1] = daftarKelass.getMurid().getNama();
            isi[x][2] = daftarKelass.getMurid().getBank().getNama();
            x++;
        }
        new ServiceHelper().setAutoRize(isi, judul, tableDaftarKelas);
    }

    private void refreshKelulusan(Kelas kelas) {
        try {
            listDaftarKelas = daftarKelasDAO.getDataByEqualsOrderByBankAndNama(kelas.getTransactionReference());
            updateTableDaftarKelulusanKelas();
        } catch (Exception ex) {
            Logger.getLogger(DaftarKelasDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateTableDaftarKelulusanKelas() {
        String[] judul = {"Choose", "No", "Nama", "Bank", "Lulus", "Hadir Hari 1", "Keterangan Hari 1", "Hadir Hari 2", "Keterangan Hari 2", "Tanggal Ujian"};
        Object[][] isi = new Object[listDaftarKelas.size()][10];
        int x = 0;
        int no = 0;
        for (DaftarKelas daftarKelass : listDaftarKelas) {
            no += 1;
            isi[x][0] = daftarKelass.isChoose();
            isi[x][1] = no;
            isi[x][2] = daftarKelass.getMurid().getNama();
            isi[x][3] = daftarKelass.getMurid().getBank().getNama();
            isi[x][4] = daftarKelass.getStatus().equals("1") ? "Y" : "N";
            isi[x][5] = daftarKelass.getKehadiran().equals("1") ? "Y" : "N";
            isi[x][6] = daftarKelass.getKeterangan();
            if (daftarKelass.getKelas().getTanggalKelas2() == null) {
                isi[x][7] = "";
                isi[x][8] = "";
            } else {
                isi[x][7] = daftarKelass.getKehadiran2().equals("1") ? "Y" : "N";
                isi[x][8] = daftarKelass.getKeterangan2();
            }
            isi[x][9] = daftarKelass.getUjian();
            x++;
        }
        tableDaftarKelas.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return columnIdentifiers.get(columnIndex).equals("Choose");
            }

            @Override
            public Class getColumnClass(int columnIndex) {
                if (columnIdentifiers.get(columnIndex).equals("Choose")) {
                    return Boolean.class;
                } else {
                    return columnIdentifiers.get(columnIndex).getClass();
                }
            }
        });
//        new ServiceHelper().setAutoRize(isi, judul, tableDaftarKelas);
    }

    private void updateTableDaftarKelasInvoice() {
        String[] judul = {"No", "Nama", "Bank", "Lulus", "Hadir Hari 1", "Keterangan Hari 1", "Hadir Hari 2", "Keterangan Hari 2", "Tanggal Ujian"};
        Object[][] isi = new Object[listDaftarKelas.size()][9];
        int x = 0;
        int no = 0;
        for (DaftarKelas daftarKelass : listDaftarKelas) {
            no += 1;
            isi[x][0] = no;
            isi[x][1] = daftarKelass.getMurid().getNama();
            isi[x][2] = daftarKelass.getMurid().getBank().getNama();
            isi[x][3] = daftarKelass.getStatus().equals("1") ? "Y" : "N";
            isi[x][4] = daftarKelass.getKehadiran().equals("1") ? "Y" : "N";
            isi[x][5] = daftarKelass.getKeterangan();
            if (daftarKelass.getKelas().getTanggalKelas2() == null) {
                isi[x][6] = "";
                isi[x][7] = "";
            } else {
                isi[x][6] = daftarKelass.getKehadiran2().equals("1") ? "Y" : "N";
                isi[x][7] = daftarKelass.getKeterangan2();
            }
            isi[x][8] = daftarKelass.getUjian();
            x++;
        }
        new ServiceHelper().setAutoRize(isi, judul, tableDaftarKelas);
    }

    private void renderButtonAksesKelulusan(List<AksesMatrix> listAksesMatrix) {
//        buttonKelulusan.setEnabled(MenuAksesConstant.validate(MenuAksesConstant.UBAH_KELULUSAN, listAksesMatrix));
    }
}
